package com.sm.tools;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.sm.R;
import com.sm.bo.Course;

public class MyCustomCourseAdapter extends ArrayAdapter<Course> {

	private ArrayList<Course> courseList;

	ViewHolder holder;
	
	private int selectedIndex = -1;

	public int getSelectedIndex() {
		return selectedIndex;
	}

	public void setSelectedIndex(int selectedIndex) {
		this.selectedIndex = selectedIndex;
	}

	public MyCustomCourseAdapter(Context context, int textViewResourceId,
			ArrayList<Course> courseList) {
		super(context, textViewResourceId, courseList);
		this.courseList = new ArrayList<Course>();
		this.courseList.addAll(courseList);
	}

	private class ViewHolder {
		TextView code;
		//CheckBox wording;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		holder = null;
		Log.v("ConvertView", String.valueOf(position));

		if (convertView == null) {
			LayoutInflater vi = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = vi.inflate(R.layout.courselist_view, null);

			holder = new ViewHolder();
			holder.code = (TextView) convertView.findViewById(R.id.code);
		//	holder.wording = (CheckBox) convertView.findViewById(R.id.checkBox1);
			convertView.setTag(holder);

//			holder.wording.setOnClickListener(new View.OnClickListener() {
//				public void onClick(View v) {
//					CheckBox cb = (CheckBox) v;
//					Course course = (Course) cb.getTag();
//					Toast.makeText(
//							getContext(),
//							"Clicked on Checkbox: " + cb.getText() + " is "
//									+ cb.isChecked(), Toast.LENGTH_LONG)
//							.show();
//					course.setSelected(cb.isChecked());
//				}
//			});

		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		if (selectedIndex == position) {
			//holder.wording.setBackgroundColor(Color.GREEN);
			holder.code.setBackgroundColor(Color.GREEN);
		} else {

		}
		Course course = courseList.get(position);
		holder.code.setText(" (" + course.getCode() + ")");
//		holder.wording.setText(course.getWording());
//		holder.wording.setChecked(course.isSelected());
//		holder.wording.setTag(course);

		return convertView;

	}

}